/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package treegen;

import java.util.ArrayList;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.control.Accordion;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TitledPane;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;

/**
 *
 * @author Елена
 */
public class BranchPane {
    VBox tabCont;
    ComboBox combo;
    HBox levels;
    VBox descLevels;
    final Button clean;
    ArrayList<Branch> branches = new ArrayList<>();
    ArrayList<BranchLevel> branchLevels = new ArrayList<>();
    Accordion accordion = new Accordion();
    int count;
    Tree tree;
    
    BranchPane (Tree tree)
    {
        count = 0;
        this.tree = tree;
        tabCont = new VBox();
        levels = new HBox();
        // Виджет для числа уровней
        Text textLevel = new Text("Количество уровней: ");
        textLevel.setFont(Font.font("Verdana", FontPosture.ITALIC, 14));
        descLevels = new VBox();
        descLevels.getChildren().add(accordion);
        combo = new ComboBox();
        combo.getItems().addAll(0,1,2,3,4,5);
        combo.setValue(0);
        combo.getSelectionModel().selectedItemProperty().addListener(
            new ChangeListener() {
            @Override
                public void changed(ObservableValue ov, Object t, Object t1) {
                    build();
                }
            });
        levels.getChildren().addAll(textLevel, combo);
        levels.setPadding(new Insets(15, 0, 10, 10));
        
        Text text = new Text ("Сбросить");
        text.setFont(Font.font("Verdana", FontWeight.BOLD, 14));
        HBox button = new HBox();
        clean = getButton("Сбросить");
        clean.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                clean();
            }
        });

        button.getChildren().add(clean);
        button.setPadding(new Insets(20, 0, 0, 10));
        tabCont.getChildren().addAll(levels, descLevels, button);
        tabCont.setPrefSize(450, 750);
        //tabCont.setStyle("-fx-background-color:gainsboro;");
    }
    
    public void clean(){
        combo.setValue(0);
        accordion.getPanes().clear();
    }
    
    public void build() {
        int oldCount = count;
        String str = combo.getValue().toString();
        count = new Integer(str);
        if (count == 0) {
            accordion.getPanes().clear();
        } else if (oldCount > count) {
            for (int i = oldCount - 1; i >= count; i--)
            {
                accordion.getPanes().remove(i);
                tree.branches.remove(i);
                Branch.setLevelCount(count);
            }
        } else if (oldCount < count) {
            for (int i = accordion.getPanes().size(); i < count; i++) {
                TitledPane t1 = new TitledPane();
                Integer levNum = i+1;
                t1.setText("Уровень " + levNum.toString());
                BranchLevel level = new BranchLevel(i, tree);
                branchLevels.add(level);
                t1.setContent(level.mainPane);
                accordion.getPanes().add(t1);
                tree.branches.add(new Branch(count, i));
            }
        }
    }
    
    /** 
     * Получить кнопку в нужном стиле
     * @param text текст кнопки
     * @return кнопка
     */
    private Button getButton (String text) {
        Button button = new Button(text);
        button.setStyle("-fx-background-color:grey;");
        button.setTextFill(Color.WHITE);
        button.setFont(Font.font("Verdana", FontWeight.BOLD, 12));
        return button;
    }
    
}
